﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ModularFixture.CApMFExcelManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;


namespace DnProcessSimulateCommands.ModularFixture;

internal class CApMFExcelManager
{
  private CultureInfo m_oldCultureInfo;
  private Microsoft.Office.Interop.Excel.Application m_excelApp;
  private _Workbook m_workbook;
  private _Worksheet m_sheet;

  public CApMFExcelManager()
  {
    this.m_oldCultureInfo = Thread.CurrentThread.CurrentCulture;
    this.m_excelApp = (Microsoft.Office.Interop.Excel.Application) null;
    this.m_workbook = (_Workbook) null;
    this.m_sheet = (_Worksheet) null;
  }

  public bool StartExcel()
  {
    bool flag = false;
    try
    {
      this.SetThreadCultureInfo(new CultureInfo("en-US"));
      if (this.m_excelApp == null)
      {
        this.m_excelApp = (Microsoft.Office.Interop.Excel.Application) Activator.CreateInstance(Marshal.GetTypeFromCLSID(new Guid("00024500-0000-0000-C000-000000000046")));
        this.m_excelApp.UserControl = true;
      }
      else if (this.m_workbook != null)
      {
        // ISSUE: reference to a compiler-generated method
        this.m_workbook.Close((object) false, Type.Missing, Type.Missing);
        this.m_sheet = (_Worksheet) null;
      }
      // ISSUE: reference to a compiler-generated method
      this.m_workbook = (_Workbook) this.m_excelApp.Workbooks.Add((object) Missing.Value);
      if (this.m_workbook != null)
      {
        this.m_sheet = (_Worksheet) (this.m_workbook.Sheets[(object) 1] as Worksheet);
        if (this.m_sheet != null)
          flag = true;
      }
    }
    catch (COMException ex)
    {
    }
    return flag;
  }

  public bool SetExcelVisibility(bool visibilityStatus)
  {
    bool flag = false;
    if (this.m_excelApp != null)
    {
      this.m_excelApp.Visible = visibilityStatus;
      flag = visibilityStatus.Equals(this.m_excelApp.Visible);
    }
    return flag;
  }

  public void ReleaseExcel()
  {
    if (this.m_sheet != null)
    {
      Marshal.ReleaseComObject((object) this.m_sheet);
      this.m_sheet = (_Worksheet) null;
    }
    if (this.m_workbook != null)
    {
      Marshal.ReleaseComObject((object) this.m_workbook);
      this.m_workbook = (_Workbook) null;
    }
    if (this.m_excelApp != null)
    {
      Marshal.ReleaseComObject((object) this.m_excelApp);
      this.m_excelApp = (Microsoft.Office.Interop.Excel.Application) null;
    }
    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();
    this.ResetThreadCultureInfo();
  }

  public void QuitExcel()
  {
    if (this.m_excelApp != null)
    {
      if (this.m_sheet != null)
      {
        Marshal.ReleaseComObject((object) this.m_sheet);
        this.m_sheet = (_Worksheet) null;
      }
      if (this.m_workbook != null)
      {
        // ISSUE: reference to a compiler-generated method
        this.m_workbook.Close((object) false, (object) Missing.Value, (object) Missing.Value);
        Marshal.ReleaseComObject((object) this.m_workbook);
        this.m_workbook = (_Workbook) null;
      }
      // ISSUE: reference to a compiler-generated method
      this.m_excelApp.Workbooks.Close();
      // ISSUE: reference to a compiler-generated method
      this.m_excelApp.Quit();
      Marshal.ReleaseComObject((object) this.m_excelApp);
      this.m_excelApp = (Microsoft.Office.Interop.Excel.Application) null;
      GC.Collect();
      GC.WaitForPendingFinalizers();
      GC.Collect();
    }
    this.ResetThreadCultureInfo();
  }

  public bool SetCellValue(int row, int column, object value)
  {
    bool flag = false;
    if (value != null && this.m_sheet != null)
    {
      if (this.m_sheet.Cells[(object) row, (object) column] is Range cell)
      {
        cell.Value2 = value;
        flag = true;
      }
      Marshal.ReleaseComObject((object) cell);
    }
    return flag;
  }

  public Range GetRange(int firstRow, int firstCol, int lastRow, int lastCol)
  {
    // ISSUE: variable of a compiler-generated type
    Range range = (Range) null;
    if (firstRow >= 1 && firstCol >= 1 && lastRow >= firstRow && lastCol >= firstCol)
    {
      if (firstRow == lastRow && firstCol == lastCol)
      {
        range = this.m_sheet.Cells[(object) firstRow, (object) firstCol] as Range;
      }
      else
      {
        string cell1 = this.ConvertToCell(firstRow, firstCol);
        string cell2 = this.ConvertToCell(lastRow, lastCol);
        if (cell1 != null && cell2 != null)
        {
          // ISSUE: reference to a compiler-generated method
          range = this.m_sheet.get_Range((object) cell1, (object) cell2);
        }
      }
    }
    return range;
  }

  public void ResetThreadCultureInfo()
  {
    Thread.CurrentThread.CurrentCulture = this.m_oldCultureInfo;
  }

  public void SetThreadCultureInfo(CultureInfo newCultureInfo)
  {
    Thread.CurrentThread.CurrentCulture = newCultureInfo;
  }

  public bool ExportData(List<CApMFExportData> fixtures, List<CApMFExportData> plps)
  {
    bool flag = false;
    if (this.m_excelApp != null)
    {
      this.SetCellValue(1, 1, (object) "PLP/Frame");
      this.SetCellValue(1, 2, (object) "PLP/Frame Location");
      this.SetCellValue(1, 3, (object) "Fixture Unit");
      this.SetCellValue(1, 4, (object) "Fixture Unit Location");
      this.SetCellValue(1, 5, (object) "Target Resource");
      this.SetCellValue(1, 6, (object) "Valid Solution");
      this.SetCellValue(1, 7, (object) "Fixture Unit Usage");
      this.SetCellValue(1, 8, (object) "PLP Usage");
      // ISSUE: variable of a compiler-generated type
      Range range1 = (Range) null;
      int row = 2;
      for (int index = 0; index < fixtures.Count; ++index)
      {
        if (fixtures[index] is CApMFFixtureExportData fixture)
        {
          this.SetCellValue(row, 1, (object) fixture.PlpFrame);
          this.SetCellValue(row, 2, (object) fixture.PlpFrameLocation);
          this.SetCellValue(row, 3, (object) fixture.FixtureUnit);
          this.SetCellValue(row, 4, (object) fixture.FixtureUnitLocation);
          this.SetCellValue(row, 5, (object) fixture.TargetResource);
          this.SetCellValue(row, 6, (object) fixture.ValidSolution);
          this.SetCellValue(row, 7, (object) fixture.FixtureUnitUsage);
          this.SetCellValue(row, 8, (object) fixture.PlpUsage);
          ++row;
        }
      }
      if (row > 2)
      {
        // ISSUE: variable of a compiler-generated type
        Range range2 = this.GetRange(2, 6, row - 1, 6);
        if (range2 != null)
        {
          range2.HorizontalAlignment = (object) XlHAlign.xlHAlignCenter;
          Marshal.ReleaseComObject((object) range2);
          range1 = (Range) null;
        }
        // ISSUE: variable of a compiler-generated type
        Range range3 = this.GetRange(2, 3, row - 1, 5);
        if (range3 != null)
        {
          range3.HorizontalAlignment = (object) XlHAlign.xlHAlignLeft;
          Marshal.ReleaseComObject((object) range3);
          range1 = (Range) null;
        }
        // ISSUE: variable of a compiler-generated type
        Range range4 = this.GetRange(2, 7, row - 1, 7);
        if (range4 != null)
        {
          range4.HorizontalAlignment = (object) XlHAlign.xlHAlignLeft;
          Marshal.ReleaseComObject((object) range4);
          range1 = (Range) null;
        }
      }
      for (int index = 0; index < plps.Count; ++index)
      {
        if (plps[index] is CApMFPlpExportData plp)
        {
          this.SetCellValue(row, 1, (object) plp.PlpFrame);
          this.SetCellValue(row, 2, (object) plp.PlpFrameLocation);
          this.SetCellValue(row, 8, (object) plp.PlpUsage);
          ++row;
        }
      }
      if (row > 2)
      {
        // ISSUE: variable of a compiler-generated type
        Range range5 = this.GetRange(2, 1, row - 1, 2);
        if (range5 != null)
        {
          range5.HorizontalAlignment = (object) XlHAlign.xlHAlignLeft;
          Marshal.ReleaseComObject((object) range5);
          range1 = (Range) null;
        }
        // ISSUE: variable of a compiler-generated type
        Range range6 = this.GetRange(2, 8, row - 1, 8);
        if (range6 != null)
        {
          range6.HorizontalAlignment = (object) XlHAlign.xlHAlignLeft;
          Marshal.ReleaseComObject((object) range6);
          range1 = (Range) null;
        }
      }
      // ISSUE: variable of a compiler-generated type
      Range range7 = this.GetRange(1, 1, 1, 8);
      if (range7 != null)
      {
        range7.Font.Bold = (object) true;
        range7.HorizontalAlignment = (object) XlHAlign.xlHAlignCenter;
        range7.VerticalAlignment = (object) XlVAlign.xlVAlignCenter;
        // ISSUE: reference to a compiler-generated method
        range7.EntireColumn.AutoFit();
        range7.EntireColumn.NumberFormat = (object) "@";
        Marshal.ReleaseComObject((object) range7);
        range1 = (Range) null;
      }
      flag = true;
    }
    return flag;
  }

  public bool SaveExportData(List<CApMFExportData> fixtures, List<CApMFExportData> plps)
  {
    bool flag1 = false;
    if (this.m_excelApp != null)
    {
      int firstPositiveInt = this.ExtractFirstPositiveInt(this.m_excelApp.Version);
      bool flag2 = false;
      string str;
      if (firstPositiveInt >= 12)
      {
        str = "Microsoft Excel Workbook(*.xls;*.xlsx)|*.xls;*.xlsx||";
        flag2 = true;
      }
      else
        str = "Microsoft Excel Workbook(*.xls)|*.xls||";
      SaveFileDialog saveFileDialog = new SaveFileDialog();
      saveFileDialog.CheckPathExists = true;
      saveFileDialog.DefaultExt = "xls";
      saveFileDialog.FileName = "MappingReport.xls";
      saveFileDialog.Filter = str;
      saveFileDialog.OverwritePrompt = true;
      saveFileDialog.AddExtension = true;
      if (saveFileDialog.ShowDialog() == DialogResult.OK)
      {
        this.m_excelApp.DisplayAlerts = false;
        flag1 = this.ExportData(fixtures, plps);
        if (flag1)
        {
          bool flag3 = false;
          // ISSUE: variable of a compiler-generated type
          XlFileFormat FileFormat = XlFileFormat.xlWorkbookNormal;
          string lower = saveFileDialog.FileName.ToLower();
          if (lower.EndsWith(".xlsx"))
          {
            flag3 = true;
            flag1 = flag2;
          }
          else if (lower.EndsWith(".xls") && flag2)
            FileFormat = XlFileFormat.xlAddIn;
          if (flag1)
          {
            if (flag3)
            {
              // ISSUE: reference to a compiler-generated method
              this.m_workbook._SaveAs((object) saveFileDialog.FileName, (object) Missing.Value, (object) Missing.Value, (object) Missing.Value, (object) Missing.Value, (object) Missing.Value, ConflictResolution: (object) Missing.Value, AddToMru: (object) Missing.Value, TextCodepage: (object) Missing.Value, TextVisualLayout: (object) Missing.Value);
            }
            else
            {
              // ISSUE: reference to a compiler-generated method
              this.m_workbook._SaveAs((object) saveFileDialog.FileName, (object) FileFormat, (object) Missing.Value, (object) Missing.Value, (object) Missing.Value, (object) Missing.Value, ConflictResolution: (object) Missing.Value, AddToMru: (object) Missing.Value, TextCodepage: (object) Missing.Value, TextVisualLayout: (object) Missing.Value);
            }
          }
        }
      }
    }
    return flag1;
  }

  private int ExtractFirstPositiveInt(string data)
  {
    int firstPositiveInt = -1;
    if (data != null)
    {
      string s = "";
      bool flag = false;
      for (int index = 0; index < data.Length; ++index)
      {
        if (data[index] >= '0' && data[index] <= '9')
        {
          s += data[index].ToString();
          flag = true;
        }
        else if (flag)
          break;
      }
      if (flag)
      {
        try
        {
          firstPositiveInt = int.Parse(s);
        }
        catch (Exception ex)
        {
          firstPositiveInt = -1;
        }
      }
    }
    return firstPositiveInt;
  }

  private string ConvertToCell(int row, int column)
  {
    string cell = "";
    if (row > 0 && column > 0)
    {
      int num1 = column % 26;
      string str = cell + ((char) (65 + num1 - 1)).ToString();
      int num2 = (column - num1) / 26 % 26;
      if (num2 > 0)
        str = ((char) (65 + num2 - 1)).ToString() + str;
      cell = str + row.ToString();
    }
    return cell;
  }
}
